package cn.bdqn.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.bdqn.pojo.Department;
import cn.bdqn.pojo.Post;
import cn.bdqn.pojo.Title;
import cn.bdqn.service.DeptService;
import cn.bdqn.service.PostService;
import cn.bdqn.service.TitleService;
import cn.bdqn.tools.Page;

@Controller
@RequestMapping("/title")
public class TitleController {
    private List<Post> postList;
    private List<Title> titleList;
    private List<Department> deptList;
    @Resource
    private PostService postService;

    @Resource
    private TitleService titleService;

    @Resource
    private DeptService deptService;

    //查询所有职称
    @RequestMapping("findtitle")
    public String findTitle(Model model, @RequestParam(required=false) String titleName,
                            @RequestParam(required=false,defaultValue="0") Integer deptId, @RequestParam(required=false,defaultValue="2") Integer isEnable,
                            @RequestParam(value="pageIndex",required=false,defaultValue="1") Integer currentPageNo){
        if("0".equals(Integer.toString(deptId))){
            deptId=null;
        }
        if("2".equals(Integer.toString(isEnable))){
            isEnable=null;
        }
        if("".equals(titleName)){
            titleName=null;
        }
        Integer pageSize = 2;
        int totalCount = 0;
        totalCount = titleService.count(titleName, deptId, isEnable);
        Page pages = new Page();
        pages.setCurrPageNo(currentPageNo);
        pages.setPageSize(pageSize);
        pages.setTotalCount(totalCount);
        int totalPageCount = pages.getTotalPageCount();
        titleList = titleService.findTitle(titleName,deptId,isEnable,currentPageNo,pageSize);
        deptList = deptService.findDept();
        model.addAttribute("titleList",titleList);
        model.addAttribute("page",pages);
        model.addAttribute("deptList",deptList);
        model.addAttribute("totalCount",totalCount);
        return "professional-title-setting";
    }
    //新增查询所有部门
    @RequestMapping("seltitle")
    public String selTitle(Model model){
        deptList = deptService.findDept();
        model.addAttribute("deptList",deptList);
        return "professional-title-setting-add";
    }
    //新增职称
    @RequestMapping("addtitle")
    public String addTitle(Title title){
        if(title.getIsEnable()==null){
            title.setIsEnable(1);
        }
        int flag = titleService.addTitle(title);
        if(flag == 1) {
            return "redirect:/title/findtitle";
        }else {
            return "redirect:/title/seltitle";
        }
    }

    //查询职称详情信息
    @RequestMapping("gettitle")
    public String getTitle(Model model,@RequestParam Integer id){
        Title title = titleService.getTitle(id);
        if(title!=null){
            model.addAttribute("title",title);
            return "professional-title-setting-view";
        }else{
            return "redirect:/title/findtitle";
        }
    }
    //修改  获取下拉框
    @RequestMapping("findmodify")
    public String findModify(Model model,@RequestParam Integer id){
        Title title = titleService.getTitle(id);
        deptList = deptService.findDept();
        postList = postService.findPostName(title.getDeptId());
        if(title!=null && deptList!=null){
            model.addAttribute("title",title);
            model.addAttribute("deptList",deptList);
            model.addAttribute("postList",postList);
            return "professional-title-setting-modify";
        }else{
            return "redirect:/title/findtitle";
        }
    }
    //修改职称
    @RequestMapping("modifytitle")
    public String modifyTitle(Title title){
        if(title.getIsEnable()==null){
            title.setIsEnable(1);
        }
        int flag = titleService.modifyTitle(title);
        if(flag == 1) {
            return "redirect:/title/findtitle";
        }else {
            return "redirect:/title/findmodify";
        }
    }
    //删除职位信息
    @RequestMapping("deltitle")
    @ResponseBody
    public String delTitle(@RequestParam Integer titleId){
        int flag = titleService.delTitle(titleId);
        if(flag == 1) {
            return "true";
        }else {
            return "false";
        }
    }
    @RequestMapping("seldept")
    @ResponseBody
    public Object selDept(Integer deptId){
        return postService.findPostName(deptId);
    }
}
